(function(jQuery) {
	
	var funNew = null;//调用四级地址时需要条用其他方法
	
	
	/*
	 * 四级联动插件
	 */
	$.AddressPddUtils = {
			
			/**
			 * 初期化国家
			 */
			selectCountry : function(country,countryCode) {
				var effectRow = new Object();
				effectRow.Login_SessionId=sessionId;
				//获取省级下拉框值
				var countryId = $("#"+country+"").val();
        		$.post("addressNew/queryCountryForPdd.json", effectRow, function(data) {
        			
        	    	if (data.retcode == 0) {
        	    		$("#"+country+"").combobox("clear").combobox('loadData',data);
        	    		var Country = data.retdata;
        	    		 var datanew,json;
        	    		 datanew = [];
        	    		 datanew.push({ "text": "", "id":"-1"});
        	    		for(var i=0;i<Country.length;i++){
        	    			datanew.push({ "text": Country[i].countryName, "id":Country[i].country});
        	    		}
        	    		$("#"+country+"").combobox("loadData", datanew);
        	    	}
        	    	if(null!=country){
        	    		$("#"+country+"").combobox("setValue",countryCode);
        	    		countryCode=null;
        	    	}
        		}, "json");
			},
			/**
			 * 初期化省级地址
			 */
			selectProvince : function(inputId,adressCode) {
				var effectRow = new Object();
				effectRow.Login_SessionId=sessionId;
				//获取省级下拉框值
				var province = $("#"+inputId[0]+"").val();
				if(this.flag===1){
					effectRow.countryId = $("#"+this.countryId+"").combobox('getValue');
					if(null==$("#"+this.countryId+"").combobox('getValue') || ""==$("#"+this.countryId+"").combobox('getValue')){
						effectRow.countryId = "-1";
					}
				}
        		$.post("addressNew/queryProvinceForPdd.json", effectRow, function(data) {
        	    	if (data.retcode == 0) {
        	    		if(null!=inputId[0]&&inputId[0].length>0){
        	    			$("#"+inputId[0]+"").combobox("clear").combobox('loadData',data);
        	    		}
        	    		if(null!=inputId[1]&&inputId[1].length>0){
        	    			$("#"+inputId[1]+"").combobox("clear").combobox('loadData',data);
        	    		}
        	    		if(null!=inputId[2]&&inputId[2].length>0){
        	    			$("#"+inputId[2]+"").combobox("clear").combobox('loadData',data);
        	    		}
        	    		if(null!=inputId[3]&&inputId[3].length>0){
        	    			$("#"+inputId[3]+"").combobox("clear").combobox('loadData',data);
        	    		}
        	    		var Provinces = data.retdata;
        	    		 var datanew,json;
        	    		 datanew = [];
        	    		 datanew.push({ "text": "", "id":"0"});
        	    		for(var i=0;i<Provinces.length;i++){
        	    			datanew.push({ "text": Provinces[i].firstArea, "id":Provinces[i].firstCode});
        	    		}
        	    		$("#"+inputId[0]+"").combobox("loadData", datanew);
        	    	}
        	    	if(null!=adressCode){
        	    		$("#"+inputId[0]+"").combobox("setValue",adressCode[0]);
        	    		adressCode[0]=null;
        	    	}
        		}, "json");
			},
			
			/**
			 * 选取市
			 */
			selectCity : function(inputId,adressCode) {
				var effectRow = new Object();
				effectRow.Login_SessionId=sessionId;
				//选取省级下拉框值作为查询条件
				var province = $("#"+inputId[0]+"").combobox('getValue');
				effectRow.provinceId=province;
        		$.post("addressNew/queryCityForPdd.json", effectRow, function(data) {
        	    	if (data.retcode == 0) {
        	    		if(null!=inputId[1]&&inputId[1].length>0){
        	    			$("#"+inputId[1]+"").combobox("clear").combobox('loadData',data);
        	    		}
        	    		if(null!=inputId[2]&&inputId[2].length>0){
        	    			$("#"+inputId[2]+"").combobox("clear").combobox('loadData',data);
        	    		}
        	    		if(null!=inputId[3]&&inputId[3].length>0){
        	    			$("#"+inputId[3]+"").combobox("clear").combobox('loadData',data);
        	    		}
        	    		var citys = data.retdata;
       	    		 var datanew,json;
    	    		 datanew = [];
        	    		for(var i=0;i<citys.length;i++){
        	    				datanew.push({ "id":citys[i].secondCode, "text": citys[i].secondArea});
        	    		}
        	    		if(null!=inputId[1]&&inputId[1].length>0){
        	    			$("#"+inputId[1]+"").combobox("loadData", datanew);
        	    		}
        	    	}
        	    	if(null!=adressCode){
        	    		$("#"+inputId[1]+"").combobox("setValue",adressCode[1]);
        	    		adressCode[1]=null;
        	    	}
        		}, "json");
			},
			
			/**
			 * 选取区
			 */
			selectDistrict : function(inputId,adressCode) {
				var effectRow = new Object();
				effectRow.Login_SessionId=sessionId;
				var cityId = $("#"+inputId[1]+"").combobox('getValue');
				effectRow.cityId=cityId;
        		$.post("addressNew/queryDistrictForPdd.json", effectRow, function(data) {
        	    	if (data.retcode == 0) {
        	    		if(null!=inputId[2]&&inputId[2].length>0){
        	    			$("#"+inputId[2]+"").combobox("clear").combobox('loadData',data);
        	    		}
        	    		if(null!=inputId[3]&&inputId[3].length>0){
        	    			$("#"+inputId[3]+"").combobox("clear").combobox('loadData',data);
        	    		}
        	    		var districts = data.retdata;
          	    		 var datanew,json;
        	    		 datanew = [];
        	    		for(var i=0;i<districts.length;i++){
        	    				datanew.push({ "id":districts[i].thirdCode, "text": districts[i].thirdArea});
        	    		}
        	    		if(null!=inputId[2]&&inputId[2].length>0){
        	    			$("#"+inputId[2]+"").combobox("loadData", datanew);
        	    		}
        	    	}
        	    	if(null!=adressCode){
        	    		$("#"+inputId[2]+"").combobox("setValue",adressCode[2]);
        	    		adressCode[2]=null;
        	    	}
        		}, "json");
			},
			
			/**
			 * 选取街道
			 */
			selectStreet : function(inputId,adressCode) {
				var effectRow = new Object();
				effectRow.Login_SessionId=sessionId;
				var districtId = $("#"+inputId[2]+"").combobox('getValue');
				effectRow.districtId=districtId;
        		$.post("addressNew/queryStreetForPdd.json", effectRow, function(data) {
        	    	if (data.retcode == 0) {
        	    		if(null!=inputId[3]&&inputId[3].length>0){
        	    			$("#"+inputId[3]+"").combobox("clear").combobox('loadData',data);
        	    		}
        	    		var streets = data.retdata;
         	    		 var datanew,json;
        	    		 datanew = [];
        	    		for(var i=0;i<streets.length;i++){
        	    				datanew.push({ "id":streets[i].fouthCode, "text": streets[i].fouthArea});
        	    		}
        	    		if(null!=inputId[3]&&inputId[3].length>0){
        	    			$("#"+inputId[3]+"").combobox("loadData", datanew);
        	    		}
        	    	}
        	    	if(null!=adressCode){
        	    		$("#"+inputId[3]+"").combobox("setValue",adressCode[3]);
        	    		adressCode[3]=null;
        	    	}
        		}, "json");
			},
			
			/**
			 * 页面初始化函数
			 */
			inits : function(inputId,adressCode) {
				if(this.flag===0){
					if(null!=inputId&&null==adressCode){
						$.AddressPddUtils.selectProvince(inputId,null);
					}
					if(null!=inputId&&null!=adressCode){
						$.AddressPddUtils.selectProvince(inputId,adressCode);
	            	}
				}
				if(null!=inputId[0]&&inputId[0].length>0){
					$('#'+inputId[0]).combobox({
		                onChange: function (n, o) {
	                		$.AddressPddUtils.selectCity(inputId,adressCode);
		                	if(null!=funNew && funNew != undefined && funNew != ''){//触发其他方法
		                		funNew();
		                	}
		                }
		            });
				}
				if(null!=inputId[1]&&inputId[1].length>0){
					$('#'+inputId[1]).combobox({
		                onChange: function (n, o) {
		                		$.AddressPddUtils.selectDistrict(inputId,adressCode);
			                	if(null!=funNew && funNew != undefined && funNew != ''){//触发其他方法
			                		funNew();
			                	}
		                }
		            });
				}

				if(null!=inputId[2]&&inputId[2].length>0){
					$('#'+inputId[2]).combobox({
		                onChange: function (n, o) {
		                		$.AddressPddUtils.selectStreet(inputId,adressCode);
			                	if(null!=funNew && funNew != undefined && funNew != ''){//触发其他方法
			                		funNew();
			                	}
		                }
		            });
				}

				
			},
			flag : 0,
			countryId : null,
			countryCode : null,
			initsCountry : function(inputId,adressCode){
				this.flag=1;
				this.countryId = inputId.shift();
				if(null!=adressCode){
					this.countryCode = adressCode.shift();
				}
				if(this.flag===1){
					if(null!=this.countryId&&null==this.countryCode){
						$.AddressPddUtils.selectCountry(this.countryId,null);
						this.inits(inputId,null);
					}else if(null!=this.countryId&&null!=this.countryCode){
						$.AddressPddUtils.selectCountry(this.countryId,this.countryCode);
						this.inits(inputId,adressCode);
					}
					if(null!=this.countryId&&this.countryId.length>0){
						$('#'+this.countryId).combobox({
			                onChange: function (n, o) {
			                	if(n==1){
			                		$('#provinceId').combobox({ required: false }).combobox('disable');
	        			        	$('#cityId').combobox({ required: false }).combobox('disable');
	        			        	$('#countyId').combobox({ required: false }).combobox('disable');
	        			        	$('#streetId').combobox({ required: false }).combobox('disable');
			                	}else{
			                		$('#provinceId').combobox({ required: true }).combobox('enable');
	        			        	$('#cityId').combobox({ required: true }).combobox('enable');
	        			        	$('#countyId').combobox({ required: true }).combobox('enable');
	        			        	$('#streetId').combobox({ required: false }).combobox('enable');
			                	}
		                		$.AddressPddUtils.selectProvince(inputId,adressCode);
			                	if(null!=funNew && funNew != undefined && funNew != ''){//触发其他方法
			                		funNew();
			                	}
			                }
			            });
					}
				}
			},
			/**
			 * 页面初始化函数为调用其他方法添加
			 */
			initsNew : function(inputId,adressCode,fun) {
				funNew=fun;
				$.AddressPddUtils.inits(inputId,adressCode);
			},

        init : function() {
            var fourLevelAddress = new Object();
            fourLevelAddress.provinceNode = $(".province");
            fourLevelAddress.cityNode = $(".city");
            fourLevelAddress.townNode = $(".town");
            fourLevelAddress.streetNode = $(".street");

            $.AddressPddUtils.initPddProvinceNode(fourLevelAddress);
            $.AddressPddUtils.initPddCityNode(fourLevelAddress);
            $.AddressPddUtils.initPddTownNode(fourLevelAddress);
            
        },
        renderPddFourLevelAddress : function(provinceNode, cityNode, townNode,streetNode) {

            var fourLevelAddress = new Object();
            fourLevelAddress.provinceNode = provinceNode;
            fourLevelAddress.cityNode = cityNode;
            fourLevelAddress.townNode = townNode;
            fourLevelAddress.streetNode = streetNode;

            $.AddressPddUtils.initPddProvinceNode(fourLevelAddress);
            $.AddressPddUtils.initPddCityNode(fourLevelAddress);
            $.AddressPddUtils.initPddTownNode(fourLevelAddress);
           
        },
        // 初始化省节点
        initPddProvinceNode : function(fourLevelAddress) {

            // 设置省下拉
            $.AddressPddUtils.initProvinceNode_option(fourLevelAddress.provinceNode);
            // 设置省选择事件
            $.AddressPddUtils.initProvinceNode_change(fourLevelAddress);
           

        },
        // 设置省下拉
        initProvinceNode_option : function(provinceNode) {

            provinceNode.combobox({
                    panelHeight : '200',
                    url : 'mdDcRest/getPddProvinces.json?Login_SessionId='+ sessionId,
                    valueField : 'firstCode',
                    textField : 'firstArea',
                });
        },
        // 省选择框chang事件
        initProvinceNode_change : function(fourLevelAddress) {
            fourLevelAddress.provinceNode.combobox({
                onChange : function(n, o) {
                    // 清空右边“市 ，区，街”选项
                    fourLevelAddress.cityNode.combobox('clear');
                    // 更新市下拉
                    $.AddressPddUtils.updateOption(fourLevelAddress.cityNode,
                        'mdDcRest/getPddCitys.json?Login_SessionId='+ sessionId + '&code=' + n);
                }
            });
        },
        // 初始市选择框
        initPddCityNode : function(fourLevelAddress) {
            // 设置市下拉
            $.AddressPddUtils.initCityNode_option(fourLevelAddress.cityNode);
            // 设置市选择事件
            $.AddressPddUtils.initCityNode_change(fourLevelAddress);
        },
        // 市选择框chang事件
        initCityNode_change : function(fourLevelAddress) {
            fourLevelAddress.cityNode.combobox({
                onChange : function(n, o) {
                    fourLevelAddress.townNode.combobox('clear');

                    // 更新区下拉
                    $.AddressPddUtils.updateOption(fourLevelAddress.townNode,
                        'mdDcRest/getPddTowns.json?Login_SessionId='
                        + sessionId + '&code=' + n);
                }
            });
        },
        // 更新市下拉
        updateOption : function(node, url) {
            node.combobox('reload', url);
        },
        // 设置市下拉
        initCityNode_option : function(cityNode) {
            cityNode.combobox({
                panelHeight : '200',
                valueField : 'secondCode',
                textField : 'secondArea',
            });
        },   
        
        // 初始区县选择框
        initPddTownNode : function(fourLevelAddress) {
            // 设置区县下拉
            $.AddressPddUtils.initTownNode_option(fourLevelAddress.townNode);
            // 设置区县选择事件
        },
        // 设置区县下拉
        initTownNode_option : function(townNode) {
        	townNode.combobox({
                panelHeight : '200',
                valueField : 'thirdCode',
                textField : 'thirdArea',
            });
        },
	};
	
	
})(jQuery);
